package com.joyintech.config;

import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.joyintech.utils.StringUtils;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 * 配置文件
 * Created by jinjin on 2020-09-21.
 */
@Configuration
@MapperScan(basePackages ={"com.joyintech.**.mapper"})
public class MybatisPlusConfig {

    @Value("${mybatis-plus.tenant.column:'tenant_id'}")
    private String tenantIdColumn;

    //前缀忽略
    @Value("${mybatis-plus.tenant.ignore_prefix:''}")
    private String ignorePrefix;

    //后缀忽略
    @Value("${mybatis-plus.tenant.ignore_suffix:''}")
    private String ignoreSuffix;

    @Bean
    public MybatisPlusInterceptor paginationInterceptor() {
        MybatisPlusInterceptor paginationInterceptor = new MybatisPlusInterceptor();
        // 设置请求的页面大于最大页后操作， true调回到首页，false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量，默认 500 条，-1 不受限制
        // paginationInterceptor.setLimit(500);
        // 开启 count 的 join 优化,只针对部分 left join
        //paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        //多租户
     //   paginationInterceptor.addInnerInterceptor( new TenantLineInnerInterceptor(initTenantLineHandler()));
        //分页
        paginationInterceptor.addInnerInterceptor( new PaginationInnerInterceptor());
        return paginationInterceptor;
    }

    @Bean
    public GlobalConfig globalConfig() {
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setMetaObjectHandler(new MybatisPlusFillHandler());
        return globalConfig;
    }
    private TenantLineHandler initTenantLineHandler(){
        return new TenantLineHandler() {
            @Override
            public Expression getTenantId() {
                return new LongValue(1);
            }

            // 这是 default 方法,默认返回 false 表示所有表都需要拼多租户条件
            @Override
            public boolean ignoreTable(String tableName) {
                String temp = tableName.toUpperCase();
                if(temp.startsWith("ACT_")) return true;
                if(Arrays.asList(ignorePrefix.split(",")).stream().filter(temp::startsWith).findAny().orElse(null)!=null) return true;
                if(Arrays.asList(ignoreSuffix.split(",")).stream().filter(temp::endsWith).findAny().orElse(null)!=null) return true;
                return false;
            }
        };
    }
}
